package com.xja.house.controller;

import com.xja.house.entity.House;
import com.xja.house.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("house")
public class HouseController {
    @Autowired
    private HouseService houseService;

    @RequestMapping("list")
    public String list(Model model){
        try {
            List<House> houseList = houseService.findAll();
            model.addAttribute("houseList",houseList);
            return "list";

        }catch (Exception e){
            e.printStackTrace();
        }
        return "error";
    }
    @RequestMapping("guanli")
    public String guanli(Model model){
        try {
            List<House> houseList=houseService.selectList();
            System.out.println(houseList.size());
            model.addAttribute("houseList",houseList);
            return "guanli";
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }
    @RequestMapping("delete")
    public String delete(Integer id){
        try {
            houseService.deleteById(id);
            return "redirect:/house/guanli.do";
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }
    @RequestMapping("add")
    public String add(String title,Integer typeId,Integer floorage,Integer price,String createdate,String contact,String description){
        try {
            System.out.println(title);
            House house=new House();
            house.setTitle(title);
            house.setTypeId(typeId);
            house.setFloorage(floorage);
            house.setPrice(price);
            house.setCreatedate(new Date());
            house.setContact(contact);
            house.setDescription(description);
            houseService.add(house);
            return "redirect:/house/guanli.do";
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }


    @RequestMapping("dateil")
    public String dateil(Integer id,Model model){
        try {
            House house = houseService.selectByPrimaryKey(id);
            model.addAttribute("house",house);
            return "dateil";
        }catch (Exception e){
            e.printStackTrace();
        }
        return "error";
    }
}
